package com.tql;

import redis.clients.jedis.Jedis;

import java.util.Set;
import java.util.concurrent.TimeUnit;

public class TestString {
    public static void main(String[] args) {
        //连接Redis 主机号，端口号
        Jedis jedis=new Jedis("127.0.0.1",6379);
        jedis.flushDB();
        System.out.println("--------------添加数据");
        System.out.println(jedis.set("key1","value1"));
        System.out.println(jedis.set("key2","value2"));
        System.out.println(jedis.set("key3","value3"));
        System.out.println("删除键值对"+jedis.del("key1"));
        System.out.println("获取值"+jedis.get("key2"));
        System.out.println("添加数据"+jedis.set("key1","value1"));
        System.out.println("获取key1的值"+jedis.get("key1"));
        //在key3的值后面添加值，拼接成一个新的值
        System.out.println("在key3后添加值"+jedis.append("key3","value4"));
        //获取所有的key
        Set<String> set=jedis.keys("*");
        System.out.println("所有key"+set);
        System.out.println("获取key3值"+jedis.get("key3"));
        System.out.println("批量添加"+jedis.mset("key5","value5","key6","value6"));
        Set<String> set1=jedis.keys("*");
        System.out.println("所有key"+set1);
        System.out.println("获取key3的值"+jedis.get("key3"));
        System.out.println("删除key3的值"+jedis.del("key3"));
        System.out.println("获取多个值"+jedis.mget("key1","key3","key5"));
        jedis.flushDB();
        System.out.println("新增键值对防止覆盖原先值");
        System.out.println(jedis.setnx("key1","value2"));
        System.out.println(jedis.get("key1"));
        //setnx 表示如果有key 就不覆盖，没有相当于添加key value
        System.out.println(jedis.setnx("key1","value1"));
        System.out.println(jedis.get("key1"));
        System.out.println("新增键值对并设置有效时间");
        System.out.println(jedis.setex("key3",2,"value3"));
        System.out.println(jedis.get("key3"));
        try {
            TimeUnit.SECONDS.sleep(3);

        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(jedis.get("key3"));
        System.out.println(jedis.get("key3"));
        System.out.println("获取原值，更新为新值");
        System.out.println(jedis.getSet("key1","value1"));
        System.out.println(jedis.get("key1"));
        //获取 key的value的值得闭合区间内的数据
        System.out.println("获取key1值得字符串"+jedis.getrange("key1",0,-1));
    }
}
